/*
 * Powered By ljc
 * Since 2013 - 2013
 */

package com.ljc.app.action;

import cn.org.rapid_framework.web.scope.Flash;
import cn.org.rapid_framework.web.util.HttpUtils;
import com.ljc.app.model.User;
import com.ljc.app.query.UserQuery;
import com.ljc.app.service.UserManager;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import common.base.BaseStruts2Action;
import common.util.page.PageList;

import java.util.Hashtable;

/**
 * @author Liu_jc
 * @since 1.0
 */


public class UserAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	//forward paths
	protected static final String QUERY_JSP = "/pages/User/query.jsp";
	protected static final String LIST_JSP= "/pages/User/list.jsp";
	protected static final String CREATE_JSP = "/pages/User/create.jsp";
	protected static final String EDIT_JSP = "/pages/User/edit.jsp";
	protected static final String SHOW_JSP = "/pages/User/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/User/list.do";
	
	private UserManager userManager;
	
	private User user;
	Integer id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			user = new User();
		} else {
			user = (User)userManager.getById(id);
		}
	}
	
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setUserManager(UserManager manager) {
		this.userManager = manager;
	}	
	
	public Object getModel() {
		return user;
	}
	
	public void setId(Integer val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}
	
	/** 执行搜索 */
	public String list() {
		UserQuery query = newQuery(UserQuery.class,DEFAULT_SORT_COLUMNS);
		
		PageList page = userManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}
	
	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}
	
	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}
	
	/** 保存新增对象 */
	public String save() {
		userManager.save(user);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}
	
	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}
	
	/**保存更新对象*/
	public String update() {
		userManager.update(this.user);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}
	
	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			Integer id = new Integer((String)params.get("id"));
			userManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

}
